package com.lostandfound.controller.admin;


import com.lostandfound.pojo.Result;
import com.lostandfound.pojo.User;
import com.lostandfound.pojo.dto.UserDto;
import com.lostandfound.service.admin.LoginService;
import com.lostandfound.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping
    public Result login(@RequestBody User user){
        User loginUser = loginService.login(user);
        if (loginUser != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("name", loginUser.getName());
            claims.put("username", loginUser.getUserName());
            String jwt = JwtUtils.generateJwt(claims); //jwt包含了当前登录的员工信息
            log.info("=====jwt:{}",jwt);
            return Result.success(jwt);
        }
        return Result.error("用户名或用户密码错误");
    }

    /***
     * 修改密码
     * @param userDto
     * @return
     */
    @PostMapping("/update")
    public Result updatePassword(@RequestBody UserDto userDto){
        loginService.update(userDto);
        return Result.success("修改成功");
    }
}
